using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Metro.Admin.BusinessEntities;
using Metro.Admin.DataAccess;
using Metro.Security;  

namespace Metro.Admin.Services
{
    class AdminService:IAdminService 
    {
        private AdminDataSource adminDataSource = null;

        public AdminService()
        {
            adminDataSource = new AdminDataSource();
        }

        public ICollection<Parameter> GetParameters(string type)
        {
            ICollection<Parameter> list;
            list = adminDataSource.GetParameters(type);
            return list;
        }

        public void UpdateParameter(Parameter parameter)
        {
            adminDataSource.UpdateParameter(parameter);  
        }

        public void CreateParameter(Parameter parameter)
        {
            adminDataSource.CreateParameter(parameter); 
        }

        public void DeleteParameter(Parameter parameter)
        {
            adminDataSource.DeleteParameter(parameter);   
        }

        public List<Country> GetCountries()
        {
            List<Country> list;
            list = adminDataSource.GetCountries();
            return list;
        }

        public void CreateCountry(Country country)
        {
            adminDataSource.CreateCountry(country);
        }
        
        public void UpdateCountry(Country country)
        {
            adminDataSource.UpdateCountry(country); 
        }
        
        public void DeleteCountry(Country country)
        {
            adminDataSource.DeleteCountry(country); 
        }

        public void UpdateCity(City city)
        { 
            adminDataSource.UpdateCity(city); 
        }

        public void CreateCity(City city, string idCountry)
        {
            adminDataSource.CreateCity(city,idCountry); 
        }

        public void DeleteCity(City city)
        {
            adminDataSource.DeleteCity(city);
        }

        public ICollection<City> GetCities()
        {
            ICollection<City> list;
            list = adminDataSource.GetCities();
            return list;      
        }

        public void UpdateCustomer(Customer customer)
        {
            adminDataSource.UpdateCustomer(customer);  
        }
        public void CreateCustomer(Customer customer)
        {
            adminDataSource.CreateCustomer(customer);  
        }

        public void DeleteCustomer(Customer customer)
        {
            adminDataSource.DeleteCustomer(customer); 
        }

        public List<Customer> GetCustomers()
        {
            List<Customer> list;
            list = adminDataSource.GetCustomers();
            return list; 
        }

        public List<User> GetUsers()
        {
            List<User> list;
            list = adminDataSource.GetUsers();
            return list; 
        }

        public void UpdateUser(User user)
        {
            string salt = string.Empty, hash = string.Empty;

            try
            {
                user.DatePasswordExpiration = DateTime.Now.AddDays(30);
                user.State = State.Active;

                salt = Authentication.CreateSalt(5);
                hash = Authentication.CreatePasswordHash(user.Password, salt);

                user.Hash = hash;
                user.Salt = salt;

                adminDataSource.UpdateUser(user);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void CreateUser(User user)
        {
            string salt = string.Empty, hash = string.Empty; 

            try
            {
                user.DatePasswordExpiration = DateTime.Now.AddDays(30);
                user.State = State.Active;

                salt = Authentication.CreateSalt(5);
                hash = Authentication.CreatePasswordHash(user.Password, salt);
                
                user.Hash = hash;
                user.Salt = salt;
    
                adminDataSource.CreateUser(user);
            }
            catch(Exception ex) 
            {
                throw ex;
            }
        }

        public void DeleteUser(User user)
        {
            adminDataSource.DeleteUser(user); 
        }

        public List<Credit> GetCredits()
        {
            List<Credit> list;
            list = adminDataSource.GetCredits();
            return list; 
        }

        public void DeleteCredit(Credit credit)
        {
            adminDataSource.DeleteCredit(credit); 
        }

        public void CreateCredit(Credit credit)
        {
            adminDataSource.CreateCredit(credit); 
        }

    }
}
